//
//  ClassifyChooseView.m
//  ZYZB
//
//  Created by ENERGY on 2018/6/29.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "StatusChooseView.h"
#import "ClassifyCollectionViewCell.h"

#define itemH 36/WIDTH_6_SCALE
@interface StatusChooseView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic, strong)NSMutableArray * tempArr;
@property (nonatomic, strong)UIButton * btn;
@end

@implementation StatusChooseView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton * backgroundBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height)];
        [backgroundBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        backgroundBtn.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backgroundBtn];
        
        self.tempArr = [NSMutableArray array];
        //拿到所选的内容
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonTextChange:) name:@"buttonTextChange" object:nil];
    }return self;
}
#pragma mark - init
- (void)layoutWithArray:(NSArray *)arr withButton:(UIButton *)button{
    [self.tempArr removeAllObjects];
    [self.tempArr addObjectsFromArray:arr];
    unsigned long itemCount = arr.count - 1;
    self.btn = button;
    if ((int)itemCount < 0) {
        itemCount = 0;
    }
    if (self.collectionView != nil) {
        //        self.collectionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, (arr.count/3 + 1) * itemH + arr.count/3 * 12/WIDTH_6_SCALE + 24/WIDTH_6_SCALE);
        self.collectionView.frame = CGRectMake(0, -1/WIDTH_6_SCALE, SCREEN_WIDTH, (itemCount/3 + 1) * itemH + itemCount/3 * 12/WIDTH_6_SCALE + 24/WIDTH_6_SCALE);
        [self.collectionView reloadData];
        return;
    }
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(100/WIDTH_6_SCALE, itemH);
    //两行之间的距离
    layout.minimumLineSpacing = 12/WIDTH_6_SCALE;
    //两列之间的距离
    //    layout.minimumInteritemSpacing = 20/WIDTH_6_SCALE;
    layout.sectionInset = UIEdgeInsetsMake(12/WIDTH_6_SCALE, 18/WIDTH_6_SCALE, 12/WIDTH_6_SCALE, 18/WIDTH_6_SCALE);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (itemCount/3 + 1) * itemH + itemCount/3 * 12/WIDTH_6_SCALE + 24/WIDTH_6_SCALE) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ClassifyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ClassifyCollectionViewCell class])];
    [self addSubview:self.collectionView];
    //    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    //    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    [self.collectionView reloadData];
}
#pragma mark - event
- (void)showView
{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)cancelBtnClick
{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - collectionViewDelegate和collectionViewDataSource
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassifyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ClassifyCollectionViewCell class]) forIndexPath:indexPath];
    cell.model = self.tempArr[indexPath.row];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.tempArr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassifyCollectionViewCell * cell = (ClassifyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.contentLab.layer.borderColor = [UIColor orangeColor].CGColor;
//    cell.contentLab.textColor = [UIColor orangeColor];
//    [self.btn setTitle:cell.contentLab.text forState:UIControlStateNormal];
    if (self.statusChooseViewBlock) {
        self.statusChooseViewBlock(cell.contentLab.text, cell.contentID);
    }
}
//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    ClassifyCollectionViewCell * cell = (ClassifyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.contentLab.layer.borderColor = getColor(@"737373").CGColor;
//    cell.contentLab.textColor = getColor(@"a0a0a0");
//}
- (void)buttonTextChange:(NSNotification *)notification{
    NSDictionary * dic = notification.userInfo;
    [self.btn setTitle:dic[@"buttonText"] forState:UIControlStateNormal];
}
@end
